/**
 * @description: 任务统计
 *
 * @author majie
 * @date 2018年7月9日 下午21:00:52
 * @version 1.0.0.1
 */
var aemDiskStatistics = {

    accessToken : null ,
    aemName : null ,
    aemId : null ,

    init : function(accessToken,aemId,aemName){
    	aemDiskStatistics.accessToken = accessToken;
    	aemDiskStatistics.aemId = aemId;
    	aemDiskStatistics.aemName = aemName;
        return aemDiskStatistics;
    },
    //初始化统计分享数据
    initCount:function (){
    	 var data = {
    			 accessToken : aemDiskStatistics.accessToken,
    			 id : aemDiskStatistics.aemId
    	        };
	    var initCountUrl = aemDiskStatistics.getPath()+"/aem/client/ajaxDistStaticsInfo";
	    var data = JSON.parse(aemDiskStatistics.syncAjax("post", initCountUrl , data , false));   	
		//获取数据
		var initData;
		if(data.status=="200"){
			initData=data.data;
		}
		var clickCount = 0;
		var shareCount = 0;
		var sharePercent = 0;		
		if(aemDiskStatistics.StringEmpty(initData)){
			if(aemDiskStatistics.StringEmpty(initData.sharePercent)){
				sharePercent=initData.sharePercent;
			}
			if(aemDiskStatistics.StringEmpty(initData.clickCount)){
				clickCount=initData.clickCount.uv;
			}
			if(aemDiskStatistics.StringEmpty(initData.shareCount)){
				shareCount=initData.shareCount.shareUv;
			}
		}
		$(".share-count").html(shareCount);
		$(".share-percent").html(sharePercent);
		//aemDiskStatistics.setCircleStyle('defeat-opponent-circle', sharePercent/ 100);
		$(".click-count").html(clickCount);
		$(".aem-name").html(aemDiskStatistics.aemName);
    },
  //获取最近7天时间集合
    getDateTime : function (){
    	var datas = new Array();
    	datas.startDay = aemDiskStatistics.timeFormat(new Date(new Date().getTime()- 7 * 60 * 60 * 1000 * 24));
    	datas.endDay = aemDiskStatistics.timeFormat(new Date(new Date().getTime()- 1 * 60 * 60 * 1000 * 24));
    	var dateList = new Array();
    	for(var i=7;i>0;i--){
    		var date = aemDiskStatistics.timeFormat(new Date(new Date().getTime()- i * 60 * 60 * 1000 * 24))
    		dateList.push(date);
    	}
    	datas.dateList = dateList;
    	return datas;
    },
    //时间格式化
    timeFormat : function (date){
    	var day = new Date(date);
    	var y = day.getFullYear();    
    	var m = day.getMonth()+1;//获取当前月份的日期   
    	var d = day.getDate();
    	var strDate = y+"-"+m+"-"+d;
    	return strDate;
    },
    //判断字符串是否为空
    StringEmpty : function(content) {
        if (content == undefined || content == "" || content == null) {
            return false;
        } else {
            return true;
        }
    },
    //折线图控件
    visitChartLine : function (){	
    	var lineChart = echarts.init($(".click-chart")[0]);
    	lineChart.showLoading();  
    	//获取时间
    	var dates = aemDiskStatistics.getDateTime();
    	var data = {
   			 accessToken : aemDiskStatistics.accessToken,
   			 aemId : aemDiskStatistics.aemId,
   			 startDay : dates.startDay,
   			endDay : dates.endDay
   	     };
    	var visitChartUrl = aemDiskStatistics.getPath() + "/aem/client/ajaxDistVistTrend";
    	//获取数据
    	var chartData;
    	var data = JSON.parse(aemDiskStatistics.syncAjax("post", visitChartUrl , data , false));   
    	if(data.status=="200"){
    		chartData=data.data;
    	}
    	if(!aemDiskStatistics.StringEmpty(chartData)||!aemDiskStatistics.StringEmpty(chartData.visitPvList)){
    		lineChart.hideLoading();  
    		$(".click-chart").addClass("no-data-chart");
    		$(".click-chart").html("暂无数据");
    		return null;
    	}
    	//折线图数据对象
    	var chartLineData = new Object();
    	//定义图例
    	var legendData =['访问次数','访问用户'];
    	//x
    	var xData = dates.dateList;
    	//y
    	var yData = new Array();
    	//访问次数集合
    	var visitCountArray = new Array();
    	//访问用户集合
    	var visitNumArray = new Array();
    	//初始化
    	for(var i =1;i<8;i++){
    		visitCountArray.push(0);
    		visitNumArray.push(0);
    	}
    	//匹配查询数据
    	if(aemDiskStatistics.StringEmpty(chartData)&&aemDiskStatistics.StringEmpty(chartData.visitPvList)){
    		visitCountArray = new Array();
    		var visitPvList = chartData.visitPvList;
    		for(var i =0;i<xData.length;i++){
    			var count = 0;
    			var xDay=xData[i];
    			for(var j=0;j<visitPvList.length;j++){
    				var visit = visitPvList[j];
    				if(aemDiskStatistics.timeFormat(visit.createDay)==xDay){
    					count=visit.pv;
    				}
    			}
    			visitCountArray.push(count);
    		}
    	}
    	if(aemDiskStatistics.StringEmpty(chartData)&&aemDiskStatistics.StringEmpty(chartData.visitUvList)){
    		visitNumArray = new Array();
    		var visitUvList = chartData.visitUvList;
    		for(var i =0;i<xData.length;i++){
    			var count = 0;
    			var xDay=xData[i];
    			for(var j=0;j<visitUvList.length;j++){
    				var visit = visitUvList[j];
    				if(aemDiskStatistics.timeFormat(visit.createDay)==xDay){
    					count=visit.uv;
    				}
    			}
    			visitNumArray.push(count);
    		}
    	}
    	//处理x轴刻度格式
    	for(var i=0;i<xData.length;i++){
    		xData[i]=xData[i].substring(5);
    	}
    	var series = new Object();
    	series.name="访问次数";
    	series.type="line";
    	//series.stack="数量";
    	series.symbolSize=4;
    	series.connectNulls=true;
    	series.data=visitCountArray;
    	yData.push(series);
    	series = new Object();
    	series.type="line";
    	//series.stack="数量";
    	series.symbolSize=4;
    	series.connectNulls=true;
    	series.name="访问用户";
    	series.data=visitNumArray;
    	yData.push(series);
    	chartLineData.legendData=legendData;
    	chartLineData.xData=xData;
    	chartLineData.yData=yData;
    	lineChart.hideLoading();  
    	var lineOption = {
    		    tooltip: {
    		        trigger: 'axis'
    		    },
    		    legend: {
    		        data:chartLineData.legendData
    		    },
    		    grid: {
    		        left: '3%',
    		        right: '4%',
    		        bottom: '3%',
    		        containLabel: true
    		    },
    		    toolbox: {
    		        show : false
    		    },
    		    xAxis: {
    		        type: 'category',
    		        axisLabel: { //xAxis，yAxis，axis都有axisLabel属性对象 
    		        	show: true, //默认为true，设为false后下面都没有意义了 
    		        	interval: 0 //此处关键， 设置文本标签全部显示 
    		        },
    		        boundaryGap: false,
    		        minInterval: 1,
    		        data: chartLineData.xData
    		    },
    		    yAxis: {
    		        type: 'value'
    		    },
    		    series:chartLineData.yData,
    		    color:['#00B04D','#FF892F']
    		};
    	lineChart.setOption(lineOption);
    },
    //分享折线图控件
    shareChartLine : function (){	
    	var lineChart = echarts.init($(".share-chart")[0]);
    	lineChart.showLoading();  
    	//获取时间
    	var dates = aemDiskStatistics.getDateTime();  	
    	var data = {
      			 accessToken : aemDiskStatistics.accessToken,
      			 aemId : aemDiskStatistics.aemId,
      			 startDay : dates.startDay,
      			endDay : dates.endDay
      	     };
    	var visitChartUrl = aemDiskStatistics.getPath()+"/aem/client/ajaxDistShareTrend";
       	//获取数据
       	var chartData;
       	var data = JSON.parse(aemDiskStatistics.syncAjax("post", visitChartUrl , data , false));   
       	if(data.status=="200"){
       		chartData=data.data;
       	}
       	if(!aemDiskStatistics.StringEmpty(chartData)||chartData.length>0){
    		lineChart.hideLoading();  
    		$(".share-chart").addClass("no-data-chart");
    		$(".share-chart").html("暂无数据");
    		return null;
    	}
    	//折线图数据对象
    	var chartLineData = new Object();
    	//定义图例
    	var legendData =['分享用户'];
    	//x
    	var xData = dates.dateList;
    	//y
    	var yData = new Array();
    	//分享用户集合
    	var shareNumArray = new Array();
    	//初始化
    	for(var i =1;i<8;i++){
    		shareNumArray.push(0);
    	}
    	//匹配查询数据
    	if(aemDiskStatistics.StringEmpty(chartData)){
    		shareNumArray = new Array();
    		for(var i =0;i<xData.length;i++){
    			var count = 0;
    			var xDay=xData[i];
    			for(var j=0;j<chartData.length;j++){
    				var share = chartData[j];
    				if(aemDiskStatistics.timeFormat(share.createDay)==xDay){
    					count=share.shareUv;
    				}
    			}
    			shareNumArray.push(count);
    		}
    	}
    	//处理x轴刻度格式
    	for(var i=0;i<xData.length;i++){
    		xData[i]=xData[i].substring(5);
    	}
    	var series = new Object();
    	series.name="分享用户";
    	series.type="line";
    	//series.stack="数量";
    	series.symbolSize=4;
    	series.connectNulls=true;
    	series.data=shareNumArray;
    	yData.push(series);
    	chartLineData.legendData=legendData;
    	chartLineData.xData=xData;
    	chartLineData.yData=yData;
    	lineChart.hideLoading();  
    	var lineOption = {
    		    tooltip: {
    		        trigger: 'axis'
    		    },
    		    legend: {
    		        data:chartLineData.legendData
    		    },
    		    grid: {
    		        left: '3%',
    		        right: '4%',
    		        bottom: '3%',
    		        containLabel: true
    		    },
    		    toolbox: {
    		        show : false
    		    },
    		    xAxis: {
    		        type: 'category',
    		        axisLabel: { //xAxis，yAxis，axis都有axisLabel属性对象 
    		        	show: true, //默认为true，设为false后下面都没有意义了 
    		        	interval: 0 //此处关键， 设置文本标签全部显示 
    		        },
    		        boundaryGap: false,
    		        minInterval: 1,
    		        data: chartLineData.xData
    		    },
    		    yAxis: {
    		        type: 'value'
    		    },
    		    series:chartLineData.yData,
    		    color:['#FF892F']
    		};
    	lineChart.setOption(lineOption);
    },
    // aemTask.getPath()
    getPath : function(){
        var parse = aemDiskStatistics.parseUrl(window.location.href);
        var path = parse.protocol + '://' + parse.host + ':' + parse.port;
        if("scrm_sps" == parse.segments[0]){
            path +=  '/' + parse.segments[0];
        }
        return path;
    },

    trim : function(str) {
        return str.replace(/(^\s+$)/g, "");
    },

    /**
     * 发送ajax请求
     *
     * type_ : post or get
     * url_ : url
     * data_ : data_
     * async_ : sync type ,true or false
     */
    syncAjax : function(type_, url_, data_  , async_) {
        var msg_ = null;
        $.ajax({
            dataType : "text",
            type : type_,
            url : url_,
            data : data_,
            async : async_,
            success : function(msg) {
                msg_ = msg;
            },
            error : function(msg) {
                msg_ = msg;
            }
        });
        return msg_;
    },

    parseUrl : function(url) {
        var a =  document.createElement('a');
        a.href = url;
        return {
            source: url,
            protocol: a.protocol.replace(':',''),
            host: a.hostname,
            port: a.port,
            query: a.search,
            params: (function(){
                var ret = {},
                    seg = a.search.replace(/^\?/,'').split('&'),
                    len = seg.length, i = 0, s;
                for (;i<len;i++) {
                    if (!seg[i]) { continue; }
                    s = seg[i].split('=');
                    ret[s[0]] = s[1];
                }
                return ret;
            })(),
            file: (a.pathname.match(/\/([^\/?#]+)$/i) || [,''])[1],
            hash: a.hash.replace('#',''),
            path: a.pathname.replace(/^([^\/])/,'/$1'),
            relative: (a.href.match(/tps?:\/\/[^\/]+(.+)/) || [,''])[1],
            segments: a.pathname.replace(/^\//,'').split('/')
        };
    }/*,
    setCircleStyle: function(id, percent){
		if(isNaN(percent)){
			percent = 0;
		}
		var dashOffset = (1 - percent) * Math.PI * 100;
		var circle = document.getElementById(id);
		circle.setAttribute('stroke-dashoffset', dashOffset);
	}*/
}
